// pages/register/register.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountTip: '',
    pwdTip: '',
    nicknameTip: '',
    account: '',
    pwd: '',
    nickname: '',
    ac: '0',
    pc: '0',
    nc: '0'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 账号
   */
  rgsAccount: function(e) {
    var that = this;
    that.setData({
      account: e.detail.value
    });
    if(that.data.account.length < 6){
      that.setData({
        accountTip: '账号不小于6位！'
      })
    }else{
      wx.request({
        url: 'http://39.97.114.188:8081/queryUserByID',
        method:'GET',
        data:{
          account: that.data.account
        },
        success:function(res){
          var acc=res.data.account;
          console.log(acc);
          if(acc != null){
            that.setData({
              accountTip: '账号已存在！',
              ac: '0'
            })
          }else{
            that.setData({
              accountTip: '',
              ac: '1'
            })
          }
        }
      })
    }
  },

  /**
   * 密码
   */
  rgsPassword: function(e) {
    this.setData({
      pwd: e.detail.value
    });
    if(this.data.pwd.length < 8){
      this.setData({
        pwdTip: '密码不小于8位！',
        pc: '0'
      })
    }else{
      this.setData({
        pwdTip: '',
        pc: '1'
      })
    }
  },

  /**
   * 昵称
   */
  rgsNickname: function(e){
    this.setData({
      nickname: e.detail.value
    });
    if(this.data.nickname.length < 1){
      this.setData({
        nicknameTip: '昵称不能为空！',
        nc: '0'
      })
    }else{
      this.setData({
        nicknameTip: '',
        nc: '1'
      })
    }
  },

  /**
   * 点击注册按钮
   */
  rgsBtn: function(){
    var that = this;
    console.log(that.data.ac + that.data.pc + that.data.nc);
    if(that.data.ac == '1' && that.data.pc == '1' && that.data.nc == '1'){
      wx.request({
        url: 'http://39.97.114.188:8081/addUser',
        method:'GET',
        data:{
          account: that.data.account,
          password: that.data.pwd,
          nickname: that.data.nickname
        },
        success:function(res){
          console.log("回调函数:"+res.data)
          var resData = res.data;
          if(resData == true){
              wx.navigateTo({
                url: '../index/index',
              })
              wx.showToast({
                title: '注册成功',
                duration:2000
            })
          }else{
              wx.showToast({
                  title: '注册失败',
                  duration:2000
              })
          }
        },
        fail:function(res){
          wx.showToast({
            title: '网络错误',
            duration: '2000'
          })
        }
      })
    }else{
      wx.showToast({
        title: '请检查输入！',
        duration:2000
    })
    }
  }

})